package test.java.question;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 模拟线程实现死锁
 */
public class DeadLock {
    public static void main(String[] args) {
        DeadLock dl = new DeadLock();
        new Thread(dl::m1).start();
        new Thread(dl::m2).start();
    }
    private final Object o1 = new Object();
    private final Object o2 = new Object();

    void m1(){
        System.out.println(Thread.currentThread().getName() + "启动等待.....");
        synchronized (o1){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (o2){
                System.out.println("你先运行吧");
            }
        }
    }

    void m2(){
        System.out.println(Thread.currentThread().getName() + "启动等待.....");
        synchronized (o2){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (o1){
                System.out.println("你先运行吧");
            }
        }
    }
}
